package com.eeeffff.discoveryclient.discovery;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties;
import org.springframework.cloud.alibaba.nacos.NacosServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
/**
 * 
 * @author fenglibin
 *
 */
@Slf4j
public class NacosReactiveDiscoveryClient implements ReactiveDiscoveryClient {

	public static final String DESCRIPTION = "Spring Cloud Nacos Reactive Discovery Client";

	private NacosDiscoveryProperties discoveryProperties;

	public NacosReactiveDiscoveryClient(NacosDiscoveryProperties discoveryProperties) {
		this.discoveryProperties = discoveryProperties;
	}

	@Override
	public String description() {
		return DESCRIPTION;
	}

	@Override
	public Flux<ServiceInstance> getInstances(String serviceId) {
		try {
			List<Instance> instances = discoveryProperties.namingServiceInstance().selectInstances(serviceId, true);

			return Flux.fromStream(hostToServiceInstanceList(instances, serviceId).stream());
		} catch (Exception e) {
			throw new RuntimeException("Can not get hosts from nacos server. serviceId: " + serviceId, e);
		}
	}

	@Override
	public Flux<String> getServices() {
		try {
			ListView<String> services = discoveryProperties.namingServiceInstance().getServicesOfServer(1,
					Integer.MAX_VALUE);

			return Flux.fromStream(services.getData().parallelStream());
		} catch (Exception e) {
			log.error("get service name from nacos server fail,", e);
			return Flux.fromStream(Collections.<String>emptyList().stream());
		}
	}

	private static ServiceInstance hostToServiceInstance(Instance instance, String serviceId) {
		NacosServiceInstance nacosServiceInstance = new NacosServiceInstance();
		nacosServiceInstance.setHost(instance.getIp());
		nacosServiceInstance.setPort(instance.getPort());
		nacosServiceInstance.setServiceId(serviceId);

		Map<String, String> metadata = new HashMap<>();
		metadata.put("nacos.instanceId", instance.getInstanceId());
		metadata.put("nacos.weight", instance.getWeight() + "");
		metadata.put("nacos.healthy", instance.isHealthy() + "");
		metadata.put("nacos.cluster", instance.getClusterName() + "");
		metadata.putAll(instance.getMetadata());
		nacosServiceInstance.setMetadata(metadata);

		if (metadata.containsKey("secure")) {
			boolean secure = Boolean.parseBoolean(metadata.get("secure"));
			nacosServiceInstance.setSecure(secure);
		}
		return nacosServiceInstance;
	}

	private static List<ServiceInstance> hostToServiceInstanceList(List<Instance> instances, String serviceId) {
		List<ServiceInstance> result = new ArrayList<>(instances.size());
		for (Instance instance : instances) {
			result.add(hostToServiceInstance(instance, serviceId));
		}
		return result;
	}

}
